package com.sky.controller.user;

import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.HistoryOrderVO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("用户下单接口")
@RequestMapping("/user/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) throws Exception {
       OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
       log.info("用户下单 {}",orderSubmitVO);

       return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }
    @ApiOperation("分页查询历史订单")
    @GetMapping("/historyOrders")
    public Result<PageResult> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO) throws Exception {
        log.info("用户分页查询历史订单{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);

        return Result.success(pageResult);
}

    @ApiOperation("用户查询详细订单")
    @GetMapping("/orderDetail/{id}")
    public Result<HistoryOrderVO> orderDetail(@PathVariable Long id) throws Exception {
        log.info("用户查询详细订单");
        HistoryOrderVO historyOrderVO = orderService.orderDetail(id);
        return Result.success(historyOrderVO);
    }
    @ApiOperation("用户取消订单")
    @PutMapping("cancel/{id}")
    public Result cancel(@PathVariable Long id)  {
        log.info("用户取消订单 {}",id);
        orderService.cancelOrder(id);

        return Result.success();
    }

    @ApiOperation("再来一单")
    @PostMapping("repetition/{id}")
    public Result orderAgain(@PathVariable Long id)  {

        log.info("用户再来一单 {}",id);
        orderService.orderAgain(id);

        return Result.success();
    }

    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){
        log.info("用户催单");
        orderService.reminder(id);

        return Result.success();
    }
}
